<?php

/**
 * Principal property
 *
 * The principal property represents a principal from RFC3744 (ACL).
 * The property can be used to specify a principal or pseudo principals. 
 *
   * Copyright © 2010 Jack Cleaver
   * 
   * This plugin provides functionality added by CalDAV (RFC 4791)
   * It implements new reports, and the MKCALENDAR method.
   *
   * This file is part of Slash.
   *
   * Slash is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Slash is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with Slash.  If not, see <http://www.gnu.org/licenses/>.
 */
class Slash_ACL_Property_GroupMemberSet extends Sabre_DAV_Property
{

    /**
     * Url to owner
     *
     * @var string 
     */
    private $hrefs = array();

    /**
     * Creates the property.
     *
     * The 'type' argument must be one of the type constants defined in this class.
     *
     * 'href' is only required for the HREF type.
     * 
     * @param int $type 
     * @param string $href 
     * @return void
     */
    public function __construct($hrefs) 
    {
      if (is_null($hrefs)) 
      {
      	Slash_Debug::debug('The href argument must be specified for the CalendarHomeSet property.');
      }
      foreach ($hrefs as $href)
      {
      	if (get_class($href) != "Slash_ACL_Property_Href")
      	{
      		Slash_Debug::debug('All the hrefs passed to the CalendarHomeSet property must be Href objects; you gave me a '.gettype($href));
      	}
      }
      $this->hrefs = $hrefs;
    }


    /**
     * Returns the principal uri. 
     * 
     * @return string
     */
    public function getHrefs() 
    {
        return $this->hrefs;
    }

  static function unserialize(DOMElement $gmsElement) 
	{
  	//Slash_Debug::debug("GroupMemberSet unserialize: node=$gmsElement->nodeName");
		$nlhrefs = $gmsElement->getElementsByTagNameNS("urn:DAV", "href");
		$hrefs = array();
		$len = $nlhrefs->length;
		for ($i = 0; $i < $len; $i++)
		{
			$node = $nlhrefs->item($i); 			
			$clarknode = Sabre_DAV_XMLUtil::toClarkNotation($node);
  		//Slash_Debug::debug("GroupMemberSet unserialize: passing a $clarknode to Href unserialize");
			$href = Slash_ACL_Property_Href::unserialize($node);
			//Slash_Debug::debug("GroupMemberSet unserialize: Href unserialized a ".gettype($href));
			$hrefs[] = $href;
		} 
   	return new self($hrefs);
	}

	public function serialize(Sabre_DAV_Server $server, DOMElement $node) 
  {
    $prefix = $server->xmlNamespaces['DAV:'];
    $doc = $node->ownerDocument;
    foreach ($this->hrefs as $href)
   	{
   		$e = $doc->createElement("$prefix:href");
   		$href->serialize($server, $e);
   		$node->appendChild($e);
   	}
  }
  
  public function toString()
  {
  	$str="CalendarHomeSet: ";
  	foreach($this->hrefs as $href)
  	{
  		$str .= "<href>" . $href->getHref() . "</href>";
  	}
  	return $str;
  }
}
